<template>
	<view class="con pb150">
		<image mode="widthFix" class="image mt30" @click="upimgs" src="/static/update.png"></image>
		
		<view v-for="(item,index) in files" :key="index" class="pg25 bgF4F6FA mt30 ra15">
			<view class="flex-ju-b">
				<view class="flex">
					<image mode="widthFix" 
					 :src="item.name.endsWith('.pdf')?'/static/pdf.png':
					 item.name.endsWith('.xls')?'/static/elx.png':
					 item.name.endsWith('.xlsx')?'/static/elx.png':'/static/imgg.png'
					 "class="width91"></image>
					
					<view class="flex-col">
						<view class="size30 bold">
							{{item.name}}
						</view>
						<view class="size20 colorB8B8B8 mt10">
							{{(item.size / 1024 ).toFixed(0)}}KB
						</view>
					</view>
				</view>
				<image @click="tapdel(index)" mode="widthFix" src="/static/del.png" class="width32"></image>
			</view>
		</view>
		<!-- <view class="pg25 bgF4F6FA mt30 ra15">
			<view class="flex-ju-b">
				<view class="flex">
					<image mode="widthFix" src="/static/elx.png" class="width91"></image>
					<view class="flex-col">
						<view class="size30 bold">
							关于新创想科技有限公司.pdf
						</view>
						<view class="size20 colorB8B8B8 mt10">
							共12页  |  4.9M
						</view>
					</view>
				</view>
				<image mode="widthFix" src="/static/del.png" class="width32"></image>
			</view>
		</view> -->
		 
		<view v-if="files_url.length" class="posifi bottom20 width92vw">
			<button @click="upodateimg" style="background: #1580E7;" type="primary">上传</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				files:[],
				files_url:[],
				user:{}
			}
		},
		onShow() {
			this.user=uni.getStorageSync('user')
		},
		methods: {
			tapdel(index){
				this.files.splice(index,1)
				this.files_url.splice(index,1)
			},
			wxup(){
				return new Promise((resolve, reject) => {
					wx.chooseMessageFile({
						count: 9,
						 type:"file",
						success: (res) => {
							uni.showLoading({
								mask: true
							})
							
							var image = res.tempFiles
							var ff = []
							for (let i = 0; i < image.length; i++) {
								uni.uploadFile({
									url: this.apiurl + 'upload/image',
									filePath: image[i].path,
									name: 'file', 
									header: { 
										'content-type': 'application/json',
										'charset': 'UTF-8',
									  'Authori-zation':'Bearer '+( uni.getStorageSync("token") ? uni.getStorageSync("token") : '')
									},
									success: (uploadFileRes) => {
										let data = JSON.parse(uploadFileRes.data)
										if(data.status == 110002){
											uni.showToast({
												title:data.msg,
												icon:"none"
											})
											uni.hideLoading()
										}else{
											ff.push(JSON.parse(uploadFileRes.data).data.url)
											if(ff.length == image.length){
												resolve(ff)
												uni.hideLoading()
											}
										}
									},
									fail(e) {
										console.log(2333,e)
										uni.hideLoading()
										uni.showModal({
											content: "网络错误,稍后再试"
										})
									}
								})
							}
						},
					})
				})
			},
			h5up(){
				return new Promise((resolve, reject) => {
					uni.showLoading({
						mask: true
					})
					console.log( this.files_url,"fffggg")
					var image = this.files_url
					var ff = []
					for (let i = 0; i < image.length; i++) {
						uni.uploadFile({
							url: this.apiurl + 'upload/image',
							filePath: image[i],
							name: 'file', 
							header: {
								'Form-type':'routine',
							  'Authori-zation':'Bearer '+( uni.getStorageSync("token") ? uni.getStorageSync("token") : '')
							},
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data)
								if(data.status == 110002){
									uni.showToast({
										title:data.msg,
										icon:"none"
									})
									uni.hideLoading()
								}else{
									ff.push(JSON.parse(uploadFileRes.data).data.url)
									if(ff.length == image.length){
										resolve(ff)
										uni.hideLoading()
									}
								}
							},
							fail(e) {
								console.log(2333,e)
								uni.hideLoading()
								uni.showModal({
									content: "网络错误,稍后再试"
								})
							}
						})
					}
				})
			},
			upodateimg(){
				this.h5up().then(res=>{
					 this.http('company/apply',{
						 files:res
					 },'post').then(res=>{
						 uni.showToast({
						 	title:"上传成功",
							icon:"none"
						 })
						 
						 uni.redirectTo({
						 	url:"/pages/user/record"
						 })
					 })
				})
			},
			upimgs(){
				if(!uni.getStorageSync('token')){
					uni.showModal({
						content:'请先登录',
						success: (res) =>{
								if (res.confirm) {
									uni.reLaunch({
										url:"/pages/user/index"
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
					})
					return
				}
				// if(this.user.is_vip !=1){
				// 	uni.showModal({
				// 		content:'你还不是会员，是否开通？',
				// 		success: (res) =>{
				// 				if (res.confirm) {
				// 					uni.navigateTo({
				// 						url:"/pages/user/activate_vip"
				// 					})
				// 				} else if (res.cancel) {
				// 					console.log('用户点击取消');
				// 				}
				// 			}
				// 	})
				// 	return
				// }
				if(this.user.is_company !=1){
					uni.showModal({
						content:'你还未添加企业信息，现在去添加？',
						success: (res) =>{
								if (res.confirm) {
									uni.navigateTo({
										url:"/pages/user/enterprise_info"
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
					})
					return
				}
				
				if (process.env.UNI_PLATFORM === "h5") {
					uni.chooseImage({
						count: 9,
						success: (res) => {
							console.log(res)
							this.files_url=[...this.files_url,...res.tempFilePaths]
							this.files =[...this.files,...res.tempFiles]
						},
					})
					
					
				}else{
					wx.chooseMessageFile({
						count: 9,
						success: (res) => {
							let frr =[]
							
							res.tempFiles.map(item=>{
								frr.push(item.path)
							})
							
							 this.files_url=[...this.files_url,...frr]
							  this.files=[...this.files,...res.tempFiles]
						},
					})
					
					
				}
				
			}
		}
	}
</script>

<style>
page{
	background: #fff;
}
</style>
